from django.db import models
from django.contrib.auth.models import User
from datetime import datetime

# 小区管理模型
class Community(models.Model):
    name = models.CharField(max_length=200, verbose_name='小区名称')
    address = models.CharField(max_length=500, verbose_name='地址')
    building_count = models.IntegerField(default=0, verbose_name='楼栋数')
    unit_count = models.IntegerField(default=0, verbose_name='单元数')
    house_count = models.IntegerField(default=0, verbose_name='房屋数')
    parking_count = models.IntegerField(default=0, verbose_name='车位数量')
    description = models.TextField(blank=True, null=True, verbose_name='描述')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    
    class Meta:
        verbose_name = '小区信息'
        verbose_name_plural = '小区信息'
        
    def __str__(self):
        return self.name

# 业主管理模型
class Owner(models.Model):
    GENDER_CHOICES = (
        ('male', '男'),
        ('female', '女'),
    )
    
    community = models.ForeignKey(Community, on_delete=models.CASCADE, verbose_name='所属小区')
    name = models.CharField(max_length=100, verbose_name='姓名')
    gender = models.CharField(max_length=10, choices=GENDER_CHOICES, default='male', verbose_name='性别')
    phone = models.CharField(max_length=20, verbose_name='联系电话')
    id_card = models.CharField(max_length=18, verbose_name='身份证号')
    building = models.CharField(max_length=20, verbose_name='楼栋')
    unit = models.CharField(max_length=20, verbose_name='单元')
    room_number = models.CharField(max_length=20, verbose_name='房间号')
    house_area = models.DecimalField(max_digits=8, decimal_places=2, default=0, verbose_name='房屋面积')
    move_in_date = models.DateField(blank=True, null=True, verbose_name='入住日期')
    is_active = models.BooleanField(default=True, verbose_name='是否激活')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    
    class Meta:
        verbose_name = '业主信息'
        verbose_name_plural = '业主信息'
        
    def __str__(self):
        return f"{self.name} - {self.building}-{self.unit}-{self.room_number}"

# 报修管理模型
class Maintenance(models.Model):
    STATUS_CHOICES = (
        ('pending', '待处理'),
        ('processing', '处理中'),
        ('completed', '已完成'),
        ('rejected', '已拒绝'),
    )
    
    TYPE_CHOICES = (
        ('electricity', '电器故障'),
        ('water', '水管漏水'),
        ('heating', '供暖问题'),
        ('elevator', '电梯故障'),
        ('door_window', '门窗问题'),
        ('other', '其他问题'),
    )
    
    owner = models.ForeignKey(Owner, on_delete=models.CASCADE, verbose_name='业主')
    type = models.CharField(max_length=50, choices=TYPE_CHOICES, verbose_name='报修类型')
    content = models.TextField(verbose_name='报修内容')
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='pending', verbose_name='状态')
    image_url = models.URLField(blank=True, null=True, verbose_name='图片链接')
    report_time = models.DateTimeField(auto_now_add=True, verbose_name='报修时间')
    processing_time = models.DateTimeField(blank=True, null=True, verbose_name='处理时间')
    completion_time = models.DateTimeField(blank=True, null=True, verbose_name='完成时间')
    handler = models.ForeignKey(User, on_delete=models.SET_NULL, blank=True, null=True, verbose_name='处理人')
    remarks = models.TextField(blank=True, null=True, verbose_name='处理备注')
    rating = models.IntegerField(blank=True, null=True, verbose_name='满意度评分')
    feedback = models.TextField(blank=True, null=True, verbose_name='用户反馈')
    
    class Meta:
        verbose_name = '报修记录'
        verbose_name_plural = '报修记录'
        ordering = ['-report_time']
        
    def __str__(self):
        return f"{self.get_type_display()} - {self.owner.name} - {self.get_status_display()}"

# 公告管理模型
class Announcement(models.Model):
    TYPE_CHOICES = (
        ('notice', '通知'),
        ('event', '活动'),
        ('bill', '缴费通知'),
        ('maintenance', '维修公告'),
        ('other', '其他'),
    )
    
    STATUS_CHOICES = (
        ('draft', '草稿'),
        ('published', '已发布'),
        ('expired', '已过期'),
    )
    
    title = models.CharField(max_length=200, verbose_name='标题')
    content = models.TextField(verbose_name='内容')
    type = models.CharField(max_length=50, choices=TYPE_CHOICES, default='notice', verbose_name='类型')
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='draft', verbose_name='状态')
    publisher = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='发布人')
    publish_date = models.DateTimeField(auto_now_add=True, verbose_name='发布时间')
    expire_date = models.DateTimeField(blank=True, null=True, verbose_name='过期时间')
    is_sticky = models.BooleanField(default=False, verbose_name='是否置顶')
    views_count = models.IntegerField(default=0, verbose_name='浏览次数')
    
    class Meta:
        verbose_name = '公告管理'
        verbose_name_plural = '公告管理'
        ordering = ['-is_sticky', '-publish_date']
        
    def __str__(self):
        return self.title

# 系统设置模型
class SystemSetting(models.Model):
    company_name = models.CharField(max_length=200, verbose_name='公司名称')
    system_name = models.CharField(max_length=200, verbose_name='系统名称')
    contact_phone = models.CharField(max_length=20, verbose_name='联系电话')
    email = models.EmailField(blank=True, null=True, verbose_name='邮箱')
    address = models.CharField(max_length=500, verbose_name='公司地址')
    logo_url = models.URLField(blank=True, null=True, verbose_name='Logo链接')
    enable_notification = models.BooleanField(default=True, verbose_name='启用通知提醒')
    enable_self_repair = models.BooleanField(default=True, verbose_name='业主自助报修')
    enable_online_payment = models.BooleanField(default=False, verbose_name='在线缴费')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    updated_by = models.ForeignKey(User, on_delete=models.SET_NULL, blank=True, null=True, verbose_name='更新人')
    
    class Meta:
        verbose_name = '系统设置'
        verbose_name_plural = '系统设置'
        
    def __str__(self):
        return self.system_name
